package com.view.mediator
{
	import com.view.component.TwoView;

	import flash.events.Event;

	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;

	public class TwoViewMediator extends Mediator implements IMediator
	{
		public static const NAME:String='TwoViewMediator';

		public function TwoViewMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
		}

		protected function get twoView():TwoView
		{
			return viewComponent as TwoView;
		}

		override public function onRegister():void
		{
			twoView.addEventListener(TwoView.SECOND_STACK, onViewCreated);
		}

		protected function onViewCreated(event:Event):void
		{
			checkForMediator(twoView.activeView);
		}

		protected function checkForMediator(child:Object):void
		{
			twoView.visitedViews++;
			switch (child.id)
			{
				case "SubOneView":
					if (!facade.hasMediator(SubOneViewMediator.NAME))
						facade.registerMediator(new SubOneViewMediator(child));
					break;
				case "SubTwoView":
					if (!facade.hasMediator(SubTwoViewMediator.NAME))
						facade.registerMediator(new SubTwoViewMediator(child));
					break;
				case "SubThreeView":
					if (!facade.hasMediator(SubThreeViewMediator.NAME))
						facade.registerMediator(new SubThreeViewMediator(child));
					break;
			}
			if (twoView.visitedViews == twoView.numberOfViews)
			{
				twoView.removeEventListener(TwoView.SECOND_STACK, onViewCreated);
				twoView.removeMyListeners();
			}
		}
	}
}